/**
 * 
 */
package name.webdizz.gadget.four.envelope.client.presenter;

import name.webdizz.gadget.four.envelope.client.EnvelopePreferences;
import name.webdizz.gadget.four.envelope.client.domain.User;
import name.webdizz.gadget.four.envelope.client.event.Events;
import name.webdizz.gadget.four.envelope.client.util.EnvelopeProvider;
import name.webdizz.gadget.four.envelope.client.view.ILoginView;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.SetPrefsFeature;
import com.mvp4g.client.presenter.Presenter;

/**
 * @author Izzet_Mustafayev
 * 
 */
public class LoginViewPresenter extends Presenter<ILoginView> {

	private SetPrefsFeature preferenceFeature;
	private EnvelopePreferences envelopePreferences;

	@Override
	public void bind() {
		view.addSubmitHandler(new ClickHandler() {
			public void onClick(final ClickEvent event) {
				// save credentials
				String password = view.getPasswordTextBox().getValue();
				String username = view.getUsernameTextBox().getValue();
				if (!"".equals(password) && !"".equals(username)) {
					preferenceFeature.set(envelopePreferences.username(),
							username);
					preferenceFeature.set(envelopePreferences.password(),
							password);
					eventBus.dispatch(Events.CREDENTIALS_SAVED);
					User user = new User();
					user.setPassword(password);
					user.setUsername(username);
					EnvelopeProvider.instance().getEnvelope().setUser(
							user);
				}
			}
		});
	}

	/**
	 * @param preferenceFeature
	 *            the preferenceFeature to set
	 */
	public void setPreferenceFeature(final SetPrefsFeature preferenceFeature) {
		this.preferenceFeature = preferenceFeature;
	}

	/**
	 * @param envelopePreferences
	 *            the envelopePreferences to set
	 */
	public void setEnvelopePreferences(
			final EnvelopePreferences envelopePreferences) {
		this.envelopePreferences = envelopePreferences;
	}
}
